<template>
  <el-card>
    <div slot="header" style="line-height: 40px">
      <h1>编辑评论</h1>
    </div>
    <el-form
      ref="commentsForm"
      :rules="rules"
      label-width="80px"
      :model="commentsForm"
    >
      <el-form-item label="ID" v-show="false">
        <el-input v-model="commentsForm.id" disabled></el-input>
      </el-form-item>
      <!-- <el-form-item label="用户ID" prop="uid">
        <el-input v-model="commentsForm.uid"></el-input>
      </el-form-item>
      <el-form-item label="商品ID" prop="pid">
        <el-input v-model="commentsForm.pid"></el-input>
      </el-form-item> -->
      <el-form-item label="订单时间" prop="ctime">
        <el-date-picker
          v-model="commentsForm.ctime"
          type="datetime"
          placeholder="选择订单时间"
        >
        </el-date-picker> </el-form-item
      ><el-form-item label="评价内容" prop="comment">
        <el-input v-model.trim="commentsForm.comment"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import commentsApi from '@/http/commentsApi';

export default {
  data() {
    let checkuid = (rule, value, callback) => {
      commentsApi.checkuid(value).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          if (res.data.msg) {
            callback();
          } else {
            callback(new Error());
          }
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: 'error',
          });
        }
      });
    };
    let checkpid = (rule, value, callback) => {
      commentsApi.checkpid(value).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          if (res.data.msg) {
            callback();
          } else {
            callback(new Error());
          }
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: 'error',
          });
        }
      });
    };
    return {
      commentsForm: {
        uid:'',
        pid:'',
        ctime:'',
        comment:'',
        point:'',
      },
      rules: {
        uid: [
          {
            required: true,
            message: '请输入用户ID',
            trigger: 'blur',
          },
          {
            validator: checkuid,
            message: '没有这个用户ID',
            trigger: 'blur',
          },
        ],
        pid: [
          {
            required: true,
            message: '请输入商品ID',
            trigger: 'blur',
          },
          {
            validator: checkpid,
            message: '没有这个商品ID',
            trigger: 'blur',
          },
        ],
        ctime: [
          {
            required: true,
            message: '请输入评价时间',
            trigger: 'blur',
          },
        ],
          point: [
          {
            required: true,
            message: '请输入评分',
            trigger: 'blur',
          },
        ],
        comment: [
          {
            required: true,
            message: '请输入评价内容',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  created() {
    this.getEditObj();
  },
  methods: {
    onSubmit() {
      this.$refs['commentsForm'].validate(valid => {
        if (valid) {
          commentsApi.save(this.commentsForm).then(res => {
            if (res.data.code == 200) {
              this.$router.replace('/goods/comment.html');
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error',
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false; //阻止表单提交
        }
      });
    },
    onReset() {
      this.$refs['commentsForm'].resetFields();
    },
    getEditObj() {
      let id = this.$route.params.id;
      if (id) {
        commentsApi.edit(id).then(res => {
          if (res.data.code == 200) {
            this.commentsForm = res.data.data;
          }
        });
      }
    },
  },
};
</script>

<style></style>
